//
//  SKUDetailDefultCell_V4.m
//  ZITHER
//
//  Created by weixin on 2018/11/10.
//  Copyright © 2018 weixn. All rights reserved.
//

#import "SKUDetailDefultCell_V4.h"

@implementation SKUDetailDefultCell_V4

- (void)awakeFromNib {
    
    [super awakeFromNib];

    [self setBorderWithHiddem:YES Color:[UIColor colorWithHex:@"cccccc"] width:0.5 Radius:10];
    [self.viewBorder mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(0);
        make.left.equalTo(self).offset(15);
        make.right.equalTo(self).offset(-15);
        make.bottom.equalTo(self).offset(-15);
    }];
    [self setUI];
    [self setMasonry];
    
}


-(void)setModel:(skuDetailModel *)model
{
    _model = model;
}

-(void)setUI{
    [self addSubview:self.textView];//内容
    [self addSubview:self.textViewSub];//副内容
    [self addSubview:self.CommoditySynopsis];//内容
    [self addSubview:self.Cardenter];//→
    
    [self addSubview:self.labTakein];//接受报价
    [self addSubview:self.price];//价格
    
    [self addSubview:self.scrollview];//图片滑动
    
}

-(void)setMasonry{
    
    [self.labTakein mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(15);
        make.left.equalTo(self).offset(35);
    }];
    
    [self.Cardenter mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.labTakein.mas_centerY).offset(0);
        make.right.equalTo(self).offset(-30);
        make.width.mas_equalTo(7);
        make.height.width.mas_equalTo(15);

    }];
    
    [self.price mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.labTakein.mas_bottom).offset(5);
        make.left.equalTo(self).offset(35);
    }];

    
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.price.mas_bottom).offset(10);
        make.left.equalTo(self).offset(35);
        make.right.equalTo(self).offset(-35);
    }];
    
    [self.textViewSub mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.textView.mas_bottom).offset(5);
        make.left.equalTo(self).offset(35);
        make.right.equalTo(self).offset(-35);
    }];
    
    [self.CommoditySynopsis mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.textViewSub.mas_bottom).offset(15);
        make.left.equalTo(self).offset(35);
        make.right.equalTo(self).offset(-35);
    }];
//    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.price.mas_bottom).offset(5);
//        make.right.equalTo(self).offset(-35);
//        make.left.equalTo(self).offset(35);
//    }];
    [self.scrollview mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.CommoditySynopsis.mas_bottom).offset(5);
        make.right.equalTo(self.Cardenter).offset(0);
        make.left.equalTo(self).offset(35);
        make.height.mas_equalTo(70);
        make.bottom.mas_equalTo(self).offset(-40);
    }];
}



-(void)setIndexCell:(NSInteger)index{
    if (index == 1){
        
        NSString *str = [NSString stringWithFormat:@"%@个收到的报价",self.model.receivePriceCount];
        self.labTakein.text = str;//接受报价
        
        if (self.model.receiveFileList.count>0) {
            self.textView.text = self.model.receiveSourceDesc;
            self.price.text = self.model.receivePrice;
            self.textView.text = self.model.receiveSourceDesc;
            [self setScrImage:self.model.receiveFileList];
            [self.scrollview mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.textView.mas_bottom).offset(5);
                make.height.mas_equalTo(80);
            }];
        }else{
            [self.scrollview mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.textView.mas_bottom).offset(-10);
                make.height.mas_equalTo(0);
            }];
            self.scrollview.hidden = YES;
        }
    }else{
        [self setOfficialPrice];
    }
    [self setNeedsLayout];
    
}

-(void)setOfficialPrice{
    
    NSString *str = [NSString stringWithFormat:@"%@个公开货源",self.model.openSourceCount];
    //        NSString *str = @"个更多的公开货源";
    
    self.labTakein.text = str;//接受报价
    
    if (self.model.fileList.count>0) {
        [self setScrImage:self.model.fileList];
        self.price.hidden = YES;
        self.CommoditySynopsis.text = self.model.sourceDesc;

//        [self.scrollview mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self.textView.mas_bottom).offset(5);
//            make.height.mas_equalTo(80);
//            make.bottom.mas_equalTo(self).offset(-30);
//        }];
//        [self.price mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self.labTakein.mas_bottom).offset(5);
//        }];
//        [self.self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self.price.mas_bottom).offset(5);
//        }];
        
    }else{
//        [self.scrollview mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self.textView.mas_bottom).offset(25);
//            make.height.mas_equalTo(0);
//            make.bottom.mas_equalTo(self).offset(0);
//        }];
//        [self.price mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self.labTakein.mas_bottom).offset(0);
//            make.height.mas_equalTo(0);
//        }];
//        [self.self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self.price.mas_bottom).offset(0);
//        }];
        
        self.scrollview.hidden = YES;
    }
    
    [self setString];

    
}

-(void)setString{
    
    
    NSMutableString *textViewSubarr = [[NSMutableString alloc]init];
    NSMutableString *CommoditySynopsisarr = [[NSMutableString alloc]init];
    self.textViewSub.text = @"";
    self.textView.text = @"";
    
    if (self.model.bizGoodsSourceAttributes.count  >0) {

        [self.textViewSub mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.textView.mas_bottom).offset(5);
        }];


        [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.price.mas_bottom).offset(10);
        }];

    }else{

        [self.textViewSub mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.textView.mas_bottom).offset(0);
        }];


        [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.price.mas_bottom).offset(0);
        }];
    }


    for (NSDictionary *dic in self.model.bizGoodsSourceAttributes) {

        if ([dic[@"groupIn"] isEqualToString:@"status"] ) {

            //            [textViewSubarr appendString:[self parseAttribute:dic[@"itemKey"]]];
            [textViewSubarr appendString:[NSString stringWithFormat:@"%@ | ",[self parseAttribute:dic[@"itemKey"] dic:dic]]];
        }else{
            //            [CommoditySynopsisarr appendString:[self parseAttribute:dic[@"itemKey"]]];

            [CommoditySynopsisarr appendString:[NSString stringWithFormat:@"%@ ",[self parseAttribute:dic[@"itemKey"] dic:dic]]];
        }
    }
    self.textViewSub.text = textViewSubarr;
    self.textView.text = CommoditySynopsisarr;
}

-(void)setScrImage:(NSArray *)arr{
    
    self.scrollview.hidden = NO;
    
    
    [self.scrollview addScrImage:arr imageHeight:70];
    
    
}

-(UILabel *)labTakein{
    if (!_labTakein) {
        _labTakein = [[UILabel alloc]init];
        _labTakein.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:18];
    }
    return _labTakein;
}


- (BaseLab *)textView{
    if (!_textView) {
        _textView = [BaseLab initCustomLab:@"主标题主标题主标题主标题主标题主标题主标题主标题" color:HexColor(0x333333) font:[UIFont fontWithName:@"PingFangSC-Semibold" size:15]];
        _textView.numberOfLines = 0;
    }
    return _textView;
}

- (BaseLab *)textViewSub{
    if (!_textViewSub) {
        _textViewSub = [BaseLab initCustomLab:@"子标题子标题子标题子标题子标题子标题子标题子标题子标题子标题子标题子标题" color:HexColor(0x666666) font:[UIFont fontWithName:@"PingFang-SC-Regular" size:13]];
        _textViewSub.numberOfLines = 0;
    }
    
    return _textViewSub;
}

- (BaseLab *)CommoditySynopsis{
    if (!_CommoditySynopsis) {
        _CommoditySynopsis = [BaseLab initCustomLab:@"商品介绍商品介绍商品介绍商品介绍商品介绍商品介绍" color:HexColor(0x000000) font:[UIFont fontWithName:@"PingFang-SC-Regular" size:13]];
        _CommoditySynopsis.numberOfLines = 0;
    }
    return _CommoditySynopsis;
}

- (UIImageView *)Cardenter{
    if (!_Cardenter) {
        _Cardenter = [[UIImageView alloc]init];
        _Cardenter.image = [UIImage imageNamed:@"sku_更多货源报价"];
    }
    return _Cardenter;
}

- (UILabel *)price{
    if (!_price) {
        _price = [[UILabel alloc]init];
        _price.textColor = baseColorTheme;
        _price.font = [UIFont systemFontOfSize:16];
    }
    return _price;
}


- (BaseScrollView *)scrollview{
    if (!_scrollview) {
        _scrollview = [[BaseScrollView alloc]initWithFrame:CGRectMake(0, 0, 0, 100)];
    }
    return _scrollview;
}



-(NSString*)parseAttribute:(NSString*)str dic:(NSDictionary*)dic{
    
    NSString* KEY_STATUS_OF_NEW = @"statusOfNew";
    NSString* KEY_ORIGINAL_BAND = @"originalBand";
    NSString* KEY_METAL_BAND = @"metalBand";
    NSString* KEY_SET_DIAMOND = @"setDiamond";
    NSString* KEY_INSURANCE_CARD = @"insuranceCard";
    NSString* KEY_INSTRUCATION = @"instruction";
    NSString* KEY_BOX = @"box";
    NSString* KEY_DIAL_POINTER = @"dialPointer";
    NSString* KEY_SCREW_DRIVER = @"screwDriver";
    
    
    if ([str isEqualToString:KEY_STATUS_OF_NEW]) {
        return [NSString stringWithFormat:@"%@新",dic[@"itemValue"]];
    } else if ([str isEqualToString:KEY_ORIGINAL_BAND]) {
        return @"原装表带";
    } else if ([str isEqualToString:KEY_METAL_BAND]) {
        return [NSString stringWithFormat:@"金属表带%@节",dic[@"itemValue"]];
    } else if ([str isEqualToString:KEY_DIAL_POINTER]) {
        return @"调表针";
    } else if ([str isEqualToString:KEY_SET_DIAMOND]) {
        if ([str isEqualToString:(@"1")]) {
            return @"后加钻石";
        } else {
            return @"原装钻石";
        }
        
    } else if ([str isEqualToString:KEY_INSTRUCATION]) {
        return @"说明书";
    } else if ([str isEqualToString:KEY_SCREW_DRIVER]) {
        return @"螺丝刀";
    } else if ([str isEqualToString:KEY_INSURANCE_CARD]) {
        NSString *string = dic[@"itemValue"];
        if (![self isBlankString:string]) {
            return [NSString stringWithFormat:@"保卡 %@",string];
        } else {
            return @"保卡 空白保卡";
        }
    } else if ([str isEqualToString:KEY_BOX]) {
        return @"盒子";
    }
    
    return @"";
}
-  (BOOL) isBlankString:(NSString *)string {
    
    string = [NSString stringWithFormat:@"%@",string];
    if (string.length==0) {
        return YES;
    }
    
    if ([string isEqualToString:@"null"]) {
        return YES;
    }
    if (string == nil || string == NULL) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
    }
    
    return NO;
    
}


@end
